//
//  FortuneCollectionViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/23.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "FortuneCollectionViewController.h"

@interface FortuneCollectionViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) NSMutableArray *PaperArray;

@property (nonatomic, strong) NSMutableDictionary *deleteDic;
@property (nonatomic,strong) UILongPressGestureRecognizer *recgnizer;
@property (nonatomic, strong)UITapGestureRecognizer *tap;
@end

@implementation FortuneCollectionViewController

//static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    BlurImageView *blurImage = [[BlurImageView alloc] initWithFrame:kBounds];
    [self.view addSubview:blurImage];
    [self setCollectionView];
    

    // Do any additional setup after loading the view.
   
//    self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchImageDelect)];
//    [self.tap setNumberOfTouchesRequired:1];
//    self.collectionView.userInteractionEnabled = YES;
//    [self.collectionView addGestureRecognizer:self.tap];
    
    
    
}
//UIcollectionView手势方法
//- (void)touchImageDelect{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否删除" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//    [alert show];
//}
//AlertView的代理方法
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//    if (buttonIndex == 0) {
//      //  FortunePaperModel *model = self.PaperArray[];
//    // [PlayerDataBase shareDataBase]deleteFortunePaperModelPath:
//        
//        
//        
//    }else{
//        return;
//    }
//}
-(void)setCollectionView{
    //创建布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置布局属性
    layout.minimumLineSpacing = 20;
    //提取高度
    CGFloat width = self.view.frame.size.width;
    
    layout.itemSize = CGSizeMake((width - 20) / 3, 200);
    
    //创建集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册
    [self.collectionView registerClass:[FortunePaperCollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor clearColor];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete method implementation -- Return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete method implementation -- Return the number of items in the section
    return [[PlayerDataBase shareDataBase] selegetAllFormFortunePapers].count;;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FortunePaperCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    self.PaperArray = [[PlayerDataBase shareDataBase] selegetAllFormFortunePapers];
    FortunePaperModel *model = self.PaperArray[indexPath.row];
    [cell.headerView sd_setImageWithURL:[NSURL URLWithString:model.path]];
    _recgnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(loogPress:)];
    
    [cell addGestureRecognizer:_recgnizer];
    _recgnizer.minimumPressDuration = 1.5;
    _recgnizer.view.tag = indexPath.row;
    _recgnizer.delegate = self;
    return cell;
}
//长按代理方法
- (void)loogPress:(UILongPressGestureRecognizer *)press{
    if (press.state == UIGestureRecognizerStateBegan) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确认删除" delegate:self cancelButtonTitle:@"删除" otherButtonTitles:@"取消", nil];
        [alert show];
        
        
    }
    
}
//删除功能
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    NSLog(@"%ld",buttonIndex);
    if (buttonIndex == 0) {
        FortunePaperModel *mode = [[PlayerDataBase shareDataBase] model][_recgnizer.view.tag];
        [[PlayerDataBase shareDataBase]deleteFortunePaperModelPath:mode.path];
        [self.collectionView reloadData];
        
    }if (self.PaperArray.count<=1) {
        [self.PaperArray removeAllObjects];
        [[[PlayerDataBase shareDataBase] selegetAllFormFortunePapers] removeAllObjects];
        
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    FortuneDetailController *detailVC = [[FortuneDetailController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    FortunePaperModel *model = self.PaperArray[indexPath.row];
    detailVC.collectionView = self.collectionView;
    detailVC.model = model;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
